import Taro from '@tarojs/taro';
import { View, Image } from '@tarojs/components';
import { useState, useEffect } from "react";
import Card from '@/components/Card';
import { getFavoriteArticles } from '@/api/my';
import { noactivity } from '../image.js';
import css from './index.module.less';

export default function Collection() {
    const [data, setData] = useState([]);
    // 是否有收藏信息
    const [isHasCollection, setIsHasCollection] = useState(true);
    useEffect(() => {
        Taro.getStorage({
            key: 'userInfo',
            success: function (a) {
                const userInfo = a.data;
                const id = userInfo ? userInfo.id : -1;
                getFavoriteArticles(id).then((res) => {
                    if (res.code === 200) {
                        setData(res.data);
                        if (res.data.length === 0) {
                            setIsHasCollection(false);
                        }
                    }
                });
            }
        });
    }, []);
    return (
        <View className={css.app}>
            {isHasCollection ?
                <View>{data === [] ? '' : data.map((v) => {
                    return (<Card key={v.id} info={{ 'text': v.title, 'date': v.checkTime, school: v.organizationName, 'image': v.picUrl }} />);
                })}</View> :
                <View className={css.noactivity}>
                    <Image src={noactivity} />
                </View>}

        </View>
    );
}
